package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.*;
import com.lovo.stock.mapper.UserMapper;
import com.lovo.stock.mapper.UtrMapper;
import com.lovo.stock.service.IJurisdictionService;
import com.lovo.stock.service.IRoleService;
import com.lovo.stock.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserBean> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    IRoleService roleService;
    @Autowired
    IJurisdictionService jurisdictionService;
    @Autowired
    UtrMapper utrMapper;

    @Override
    public IPage<UserBean> queryUserByPage(PageInfo<UserBean> pageInfo) {
        QueryWrapper q = new QueryWrapper<>();
        //如果用户名不为空
        if (pageInfo.getData().getUserName() != null && !"".equals(pageInfo.getData().getUserName())) {
            q.like("user_name", pageInfo.getData().getUserName());
        }
        IPage<UserBean> page = userMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()), q);
        return page;
    }

    @Override
    public UserBean queryUserById(int userId) {
        UserBean userBean = userMapper.selectById(userId);
        //两表联查出用户对应的角色列表，放入角色对象中
        List<RoleBean> roleList = roleService.queryRoleByUserId(userId);
        userBean.setRoleBeans(roleList);
        List<Integer> roleIdList = new ArrayList<>();
        //取出角色列表中的角色id
        for (RoleBean r : roleList
        ) {
            roleIdList.add(r.getRoleId());
        }
        //联表查出角色对应的权限
        List<JurisdictionBean> jurisdictionList = jurisdictionService.queryJurisdictionByRoleIds(roleIdList);
        //将权限列表加入角色列表
        userBean.setJurisdictionBeans(jurisdictionList);
        return userBean;
    }

    @Override
    public UserBean queryUser(UserBean userBean) {
        QueryWrapper q=new QueryWrapper<>();
        q.eq("user_name",userBean.getUserName());
        q.eq("user_password",userBean.getUserPassword());
       return userMapper.selectOne(q);
    }

    @Override
    @Transactional
    public void addUser(UserBean userBean,List<Integer> roleIds) {
        //改用户表和用户角色中间表
        //新增用户
        userMapper.insert(userBean);
        //获取新增用户id
        int userId = userBean.getUserId();

        //修改用户表和角色中间

        List<Utr> utrList = new ArrayList<>();
        //将获取的用户id和角色id封装在utr对象里
        for (Integer i : roleIds
        ) {
            Utr utr = new Utr();
            utr.setUtrUserId(userId);
            utr.setUtrRoleId(i);
            utrList.add(utr);
        }
        utrMapper.addUserAndRole(utrList);
    }

    @Override
    @Transactional
    public void deleteUser(int id) {
        //改用户表和用户角色中间表
        //删除用户列表
        userMapper.deleteById(id);
        //删除用户角色
        QueryWrapper q = new QueryWrapper<>();
        q.eq("utr_user_id", id);
        utrMapper.delete(q);
    }


    //修改用户权限
    @Override
    @Transactional
    public void updateUser(List<Integer> roleIds, int userId) {
        //首先删除用户角色中间表的该用户
        QueryWrapper q = new QueryWrapper<>();
        q.eq("utr_user_id", userId);
        utrMapper.delete(q);

        //新增用户角色中间表
        List<Utr> utrList = new ArrayList<>();
        for (int roleId : roleIds) {
            Utr u = new Utr();
            u.setUtrUserId(userId);
            u.setUtrRoleId(roleId);
            utrList.add(u);
        }
        utrMapper.addUserAndRole(utrList);
    }

    @Override
    public boolean updateUserPassword(UserBean oldUser,UserBean newUser) {
        //通过传入的userBean查询数据库中的user是否存在，从而判断密码是否正确
        UserBean user=this.queryUser(oldUser);
        //
        if(user!=null){
            //密码正确
            userMapper.updateById(newUser);
            return true;
        }
        return false;
    }
}
